<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
	<title>Watching the status of an Elasticsearch cluster | ElasticSearch 7.7 权威指南中文版</title>
	<meta name="keywords" content="ElasticSearch 权威指南中文版, elasticsearch 7, es7, 实时数据分析，实时数据检索" />
    <meta name="description" content="ElasticSearch 权威指南中文版, elasticsearch 7, es7, 实时数据分析，实时数据检索" />
    <!-- Give IE8 a fighting chance -->
    <!--[if lt IE 9]>
    <script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
    <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
    <![endif]-->
	<link rel="stylesheet" type="text/css" href="../static/styles.css" />
	<script>
	var _link = 'watch-cluster-status.html';
    </script>
</head>
<body>
<div class="main-container">
    <section id="content">
        <div class="content-wrapper">
            <section id="guide" lang="zh_cn">
                <div class="container">
                    <div class="row">
                        <div class="col-xs-12 col-sm-8 col-md-8 guide-section">
                            <div style="color:gray; word-break: break-all; font-size:12px;">原英文版地址: <a href="https://www.elastic.co/guide/en/elasticsearch/reference/7.7/watch-cluster-status.html" rel="nofollow" target="_blank">https://www.elastic.co/guide/en/elasticsearch/reference/7.7/watch-cluster-status.html</a>, 原文档版权归 www.elastic.co 所有<br/>本地英文版地址: <a href="../en/watch-cluster-status.html" rel="nofollow" target="_blank">../en/watch-cluster-status.html</a></div>
                        <!-- start body -->
                  <div class="page_header">
<strong>重要</strong>: 此版本不会发布额外的bug修复或文档更新。最新信息请参考 <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/index.html" rel="nofollow">当前版本文档</a>。
</div>
<div id="content">
<div class="breadcrumbs">
<span class="breadcrumb-link"><a href="index.html">Elasticsearch Guide [7.7]</a></span>
»
<span class="breadcrumb-link"><a href="xpack-alerting.html">Alerting on cluster and index events</a></span>
»
<span class="breadcrumb-link"><a href="example-watches.html">Example watches</a></span>
»
<span class="breadcrumb-node">Watching the status of an Elasticsearch cluster</span>
</div>
<div class="navheader">
<span class="prev">
<a href="example-watches.html">« Example watches</a>
</span>
<span class="next">
<a href="watching-meetup-data.html">Watching event data »</a>
</span>
</div>
<div class="section xpack">
<div class="titlepage"><div><div>
<h2 class="title">
<a id="watch-cluster-status"></a>Watching the status of an Elasticsearch cluster<a class="edit_me edit_me_private" rel="nofollow" title="Editing on GitHub is available to Elastic" href="https://github.com/elastic/elasticsearch/edit/7.7/x-pack/docs/en/watcher/example-watches/example-watch-clusterstatus.asciidoc">edit</a><a class="xpack_tag" href="https://www.elastic.co/subscriptions"></a>
</h2>
</div></div></div>
<p>You can easily configure a basic watch to monitor the health of your
Elasticsearch cluster:</p>
<div class="ulist itemizedlist">
<ul class="itemizedlist">
<li class="listitem">
<a class="xref" href="watch-cluster-status.html#health-add-input" title="Schedule the watch and add an input">Schedule the watch and define an input</a> that gets the
cluster health status.
</li>
<li class="listitem">
<a class="xref" href="watch-cluster-status.html#health-add-condition" title="Add a condition">Add a condition</a> that evaluates the health status to
determine if action is required.
</li>
<li class="listitem">
<a class="xref" href="watch-cluster-status.html#health-take-action" title="Take action">Take action</a> if the cluster is RED.
</li>
</ul>
</div>
<h4>
<a id="health-add-input"></a>Schedule the watch and add an input<a class="edit_me edit_me_private" rel="nofollow" title="Editing on GitHub is available to Elastic" href="https://github.com/elastic/elasticsearch/edit/7.7/x-pack/docs/en/watcher/example-watches/example-watch-clusterstatus.asciidoc">edit</a>
</h4>
<p>A watch <a class="xref" href="trigger-schedule.html" title="Schedule trigger">schedule</a> controls how often a watch is triggered.
The watch <a class="xref" href="input.html" title="Inputs">input</a> gets the data that you want to evaluate.</p>
<p>The simplest way to define a schedule is to specify an interval. For example,
the following schedule runs every 10 seconds:</p>
<div class="pre_wrapper lang-console">
<pre class="programlisting prettyprint lang-console">PUT _watcher/watch/cluster_health_watch
{
  "trigger" : {
    "schedule" : { "interval" : "10s" } <a id="CO535-1"></a><i class="conum" data-value="1"></i>
  }
}</pre>
</div>
<div class="console_widget" data-snippet="snippets/1287.console"></div>
<div class="calloutlist">
<table border="0" summary="Callout list">
<tr>
<td align="left" valign="top" width="5%">
<p><a href="#CO535-1"><i class="conum" data-value="1"></i></a></p>
</td>
<td align="left" valign="top">
<p>Schedules are typically configured to run less frequently. This example sets
the interval to 10 seconds to you can easily see the watches being triggered.
Since this watch runs so frequently, don’t forget to <a class="xref" href="watch-cluster-status.html#health-delete" title="Delete the watch">delete the watch</a>
when you’re done experimenting.</p>
</td>
</tr>
</table>
</div>
<p>To get the status of your cluster, you can call the Elasticsearch
<a href="cluster-health.html" class="ulink" target="_top">cluster health</a> API:</p>
<div class="pre_wrapper lang-console">
<pre class="programlisting prettyprint lang-console">GET _cluster/health?pretty</pre>
</div>
<div class="console_widget" data-snippet="snippets/1288.console"></div>
<p>To load the health status into your watch, you simply add an
<a class="xref" href="input-http.html" title="HTTP input">HTTP input</a> that calls the cluster health API:</p>
<div class="pre_wrapper lang-console">
<pre class="programlisting prettyprint lang-console">PUT _watcher/watch/cluster_health_watch
{
  "trigger" : {
    "schedule" : { "interval" : "10s" }
  },
  "input" : {
    "http" : {
      "request" : {
        "host" : "localhost",
        "port" : 9200,
        "path" : "/_cluster/health"
      }
    }
  }
}</pre>
</div>
<div class="console_widget" data-snippet="snippets/1289.console"></div>
<p>If you’re using Security, then you’ll also need to supply some authentication
credentials as part of the watch configuration:</p>
<div class="pre_wrapper lang-console">
<pre class="programlisting prettyprint lang-console">PUT _watcher/watch/cluster_health_watch
{
  "trigger" : {
    "schedule" : { "interval" : "10s" }
  },
  "input" : {
    "http" : {
      "request" : {
        "host" : "localhost",
        "port" : 9200,
        "path" : "/_cluster/health",
        "auth": {
          "basic": {
            "username": "elastic",
            "password": "x-pack-test-password"
          }
        }
      }
    }
  }
}</pre>
</div>
<div class="console_widget" data-snippet="snippets/1290.console"></div>
<p>It would be a good idea to create a user with the minimum privileges required
for use with such a watch configuration.</p>
<p>Depending on how your cluster is configured, there may be additional settings
required before the watch can access your cluster such as keystores, truststores,
or certificates. For more information, see <a class="xref" href="notification-settings.html" title="Watcher settings in Elasticsearch">Watcher settings</a>.</p>
<p>If you check the watch history, you’ll see that the cluster status is recorded
as part of the <code class="literal">watch_record</code> each time the watch executes.</p>
<p>For example, the following request retrieves the last ten watch records from
the watch history:</p>
<div class="pre_wrapper lang-console">
<pre class="programlisting prettyprint lang-console">GET .watcher-history*/_search
{
  "sort" : [
    { "result.execution_time" : "desc" }
  ]
}</pre>
</div>
<div class="console_widget" data-snippet="snippets/1291.console"></div>
<h4>
<a id="health-add-condition"></a>Add a condition<a class="edit_me edit_me_private" rel="nofollow" title="Editing on GitHub is available to Elastic" href="https://github.com/elastic/elasticsearch/edit/7.7/x-pack/docs/en/watcher/example-watches/example-watch-clusterstatus.asciidoc">edit</a>
</h4>
<p>A <a class="xref" href="condition.html" title="Conditions">condition</a> evaluates the data you’ve loaded into the watch and
determines if any action is required. Since you’ve defined an input that loads
the cluster status into the watch, you can define a condition that checks that
status.</p>
<p>For example, you could add a condition to check to see if the status is RED.</p>
<div class="pre_wrapper lang-console">
<pre class="programlisting prettyprint lang-console">PUT _watcher/watch/cluster_health_watch
{
  "trigger" : {
    "schedule" : { "interval" : "10s" } <a id="CO536-1"></a><i class="conum" data-value="1"></i>
  },
  "input" : {
    "http" : {
      "request" : {
       "host" : "localhost",
       "port" : 9200,
       "path" : "/_cluster/health"
      }
    }
  },
  "condition" : {
    "compare" : {
      "ctx.payload.status" : { "eq" : "red" }
    }
  }
}</pre>
</div>
<div class="console_widget" data-snippet="snippets/1292.console"></div>
<div class="calloutlist">
<table border="0" summary="Callout list">
<tr>
<td align="left" valign="top" width="5%">
<p><a href="#CO536-1"><i class="conum" data-value="1"></i></a></p>
</td>
<td align="left" valign="top">
<p>Schedules are typically configured to run less frequently. This example sets
the interval to 10 seconds to you can easily see the watches being triggered.</p>
</td>
</tr>
</table>
</div>
<p>If you check the watch history, you’ll see that the condition result is recorded
as part of the <code class="literal">watch_record</code> each time the watch executes.</p>
<p>To check to see if the condition was met, you can run the following query.</p>
<div class="pre_wrapper lang-console">
<pre class="programlisting prettyprint lang-console">GET .watcher-history*/_search?pretty
{
  "query" : {
    "match" : { "result.condition.met" : true }
  }
}</pre>
</div>
<div class="console_widget" data-snippet="snippets/1293.console"></div>
<h4>
<a id="health-take-action"></a>Take action<a class="edit_me edit_me_private" rel="nofollow" title="Editing on GitHub is available to Elastic" href="https://github.com/elastic/elasticsearch/edit/7.7/x-pack/docs/en/watcher/example-watches/example-watch-clusterstatus.asciidoc">edit</a>
</h4>
<p>Recording <code class="literal">watch_records</code> in the watch history is nice, but the real power of
Watcher is being able to do something in response to an alert. A watch’s
<a class="xref" href="actions.html" title="Actions">actions</a> define what to do when the watch condition is true—​you
can send emails, call third-party webhooks, or write documents to an
Elasticsearch index or log when the watch condition is met.</p>
<p>For example, you could add an action to index the cluster status information
when the status is RED.</p>
<div class="pre_wrapper lang-console">
<pre class="programlisting prettyprint lang-console">PUT _watcher/watch/cluster_health_watch
{
  "trigger" : {
    "schedule" : { "interval" : "10s" }
  },
  "input" : {
    "http" : {
      "request" : {
       "host" : "localhost",
       "port" : 9200,
       "path" : "/_cluster/health"
      }
    }
  },
  "condition" : {
    "compare" : {
      "ctx.payload.status" : { "eq" : "red" }
    }
  },
  "actions" : {
    "send_email" : {
      "email" : {
        "to" : "username@example.org",
        "subject" : "Cluster Status Warning",
        "body" : "Cluster status is RED"
      }
    }
  }
}</pre>
</div>
<div class="console_widget" data-snippet="snippets/1294.console"></div>
<p>For Watcher to send email, you must configure an email account in your
<code class="literal">elasticsearch.yml</code> configuration file and restart Elasticsearch. To add an email
account, set the <code class="literal">xpack.notification.email.account</code> property.</p>
<p>For example, the following snippet configures a single Gmail account named <code class="literal">work</code>:</p>
<div class="pre_wrapper lang-yaml">
<pre class="programlisting prettyprint lang-yaml">xpack.notification.email.account:
  work:
    profile: gmail
    email_defaults:
      from: &lt;email&gt; <a id="CO537-1"></a><i class="conum" data-value="1"></i>
    smtp:
      auth: true
      starttls.enable: true
      host: smtp.gmail.com
      port: 587
      user: &lt;username&gt; <a id="CO537-2"></a><i class="conum" data-value="2"></i>
      password: &lt;password&gt; <a id="CO537-3"></a><i class="conum" data-value="3"></i></pre>
</div>
<div class="calloutlist">
<table border="0" summary="Callout list">
<tr>
<td align="left" valign="top" width="5%">
<p><a href="#CO537-1"><i class="conum" data-value="1"></i></a></p>
</td>
<td align="left" valign="top">
<p>Replace <code class="literal">&lt;email&gt;</code> with the email address from which you want to send
notifications.</p>
</td>
</tr>
<tr>
<td align="left" valign="top" width="5%">
<p><a href="#CO537-2"><i class="conum" data-value="2"></i></a></p>
</td>
<td align="left" valign="top">
<p>Replace <code class="literal">&lt;username&gt;</code> with your Gmail user name (typically your Gmail address).</p>
</td>
</tr>
<tr>
<td align="left" valign="top" width="5%">
<p><a href="#CO537-3"><i class="conum" data-value="3"></i></a></p>
</td>
<td align="left" valign="top">
<p>Replace <code class="literal">&lt;password&gt;</code> with your Gmail password.</p>
</td>
</tr>
</table>
</div>
<div class="note admon">
<div class="icon"></div>
<div class="admon_content">
<p>If you have advanced security options enabled for your email account,
        you need to take additional steps to send email from Watcher. For more
        information, see <a class="xref" href="actions-email.html#configuring-email" title="Configuring email accounts">Configuring email accounts</a>.</p>
</div>
</div>
<p>You can check the watch history or the <code class="literal">status_index</code> to see that the action was
performed.</p>
<div class="pre_wrapper lang-console">
<pre class="programlisting prettyprint lang-console">GET .watcher-history*/_search?pretty
{
  "query" : {
    "match" : { "result.condition.met" : true }
  }
}</pre>
</div>
<div class="console_widget" data-snippet="snippets/1295.console"></div>
<h4>
<a id="health-delete"></a>Delete the watch<a class="edit_me edit_me_private" rel="nofollow" title="Editing on GitHub is available to Elastic" href="https://github.com/elastic/elasticsearch/edit/7.7/x-pack/docs/en/watcher/example-watches/example-watch-clusterstatus.asciidoc">edit</a>
</h4>
<p>Since the <code class="literal">cluster_health_watch</code> is configured to run every 10 seconds, make
sure you delete it when you’re done experimenting. Otherwise, you’ll spam yourself
indefinitely.</p>
<p>To remove the watch, use the <a class="xref" href="watcher-api-delete-watch.html" title="Delete watch API">delete watch API</a>:</p>
<div class="pre_wrapper lang-console">
<pre class="programlisting prettyprint lang-console">DELETE _watcher/watch/cluster_health_watch</pre>
</div>
<div class="console_widget" data-snippet="snippets/1296.console"></div>
</div>
<div class="navfooter">
<span class="prev">
<a href="example-watches.html">« Example watches</a>
</span>
<span class="next">
<a href="watching-meetup-data.html">Watching event data »</a>
</span>
</div>
</div>

                  <!-- end body -->
                        </div>
                        <div class="col-xs-12 col-sm-4 col-md-4" id="right_col">
                        
                        </div>
                    </div>
                </div>
            </section>
        </div>
    </section>
</div>
<script src="../static/cn.js"></script>
</body>
</html>